import 'package:get/get.dart';
import '../../../function/model/plant_encyclopedia_model.dart';
import '../../../function/state/plant_encyclopedia_state.dart';

/// 植物百科详情页面控制器
/// 使用GetX进行页面逻辑分离
class PlantDetailController extends GetxController {
  // 植物百科状态管理
  final PlantEncyclopediaState plantEncyclopediaState =
      Get.find<PlantEncyclopediaState>();

  // 当前植物信息
  final Rx<PlantEncyclopediaModel?> _plant = Rx<PlantEncyclopediaModel?>(null);

  /// 获取当前植物信息
  PlantEncyclopediaModel? get plant => _plant.value;

  @override
  void onInit() {
    super.onInit();
    // 初始化页面数据
    _initializePageData();
  }

  /// 初始化页面数据
  void _initializePageData() {
    // 从路由参数中获取植物ID
    final plantId = Get.parameters['plantId'];
    if (plantId != null) {
      _loadPlantById(plantId);
    }
  }

  /// 根据ID加载植物信息
  void _loadPlantById(String plantId) {
    final plant = plantEncyclopediaState.getPlantById(plantId);
    if (plant != null) {
      _plant.value = plant;
    }
  }
}
